package com.bianmaba.activiti.controller.user;

import com.bianmaba.spring.web.controller.CrudController;
import com.bianmaba.activiti.service.process.ProcessService;
import com.bianmaba.commons.bean.result.OperationResult;
import com.bianmaba.activiti.beans.entity.user.Vacation;
import com.bianmaba.activiti.service.user.IVacationService;
import com.bianmaba.spring.mvc.config.annotation.EntityParam;
import com.bianmaba.spring.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @program: example-web-framework-extends
 * @description:订单业务层
 * @author: Chenjiabin
 * @create: 2018-04-21 11:45
 **/
@RestController
@RequestMapping(value = {"management/user/vacation"})
public class MGTVacationController extends CrudController<Vacation, String> {

    @Autowired
    private IVacationService service;
    @Autowired
    private ProcessService processService;

    @Override
    public OperationResult<Vacation> saveFromFormdata(@EntityParam Vacation entity) {
        Serializable userId = SecurityUtils.getCurrentUserId();
        String username = SecurityUtils.getCurrentUsername();
        String name = SecurityUtils.getCurrentName();
        entity.setUserId(userId.toString());
        entity.setUsername(username);
        entity.setName(name);
        entity.setCreator(username);
        OperationResult<Vacation> result = super.save(entity);
        processService.start(username, "vacation_process", new HashMap() {{
            put("days", entity.getDays());
            put("dataId", entity.getId());
            put("dataType", "vacation");
        }});
        return result;
    }


    @Override
    protected IVacationService getService() {
        return service;
    }
}
